/*
**
**		Heroes IV
**  Copyright 2001, The 3DO Company
**
**   adv_mana_vortex.cpp
*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_mana_vortex.h"

#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_sounds.h"
#include "adv_object_type.h"
#include "army.h"
#include "basic_dialog.h"
#include "bitmap_group_cache.h"
#include "dialog_mana_vortex.h"
#include "external_string.h"
#include "hero.h"
#include "music.h"
#include "object_registration.h"
#include "simple_dialog.h"
#include "stationary_adventure_object.h"

extern t_external_string const k_text_dead_heroes_ineligible;

/*
** Unnamed namespace
*/
namespace
{
    t_object_registration<t_adv_mana_vortex> k_registration( k_adv_object_mana_recharger, 
															    k_recharger_mana_vortex );
    int const k_current_version = 1;
} // Unnamed namespace

t_adv_mana_vortex::t_adv_mana_vortex (  std::string const& source )
          : t_stationary_adventure_object( source )
{
	m_mana_vortex_delay = 0;
}

void t_adv_mana_vortex::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                           t_direction direction, t_adventure_frame* frame )
{
	t_creature_array& creatures = army->get_creatures();
	int                     i;
	t_hero*                 hero;
	t_creature_array        hero_array;
	bool				    any_heroes = false;
	bool					any_useable_heroes = false;
	bool				    dead_heroes = true;

	t_sound_cache music = get_music_playing();
	stop_music();
	get_dialog_sound( k_dialog_sound_mana )->play( get_sound_volume() );

    // Can only be used by 1 hero in a 1 week period.
	if (m_mana_vortex_delay > 0)
    {
	    std::string status_text;

        status_text = get_text ("help_empty");
        ok_dialog( status_text, get_dialog_bottom( point ) );
		play_music( music );
        return;
    }

    // Check for heroes.
	for (i = 0; i < t_creature_array::k_size; i++)
	{
		hero = creatures[i].get_hero();
		if (hero == 0)
			continue;
		any_heroes = true;

		if (hero->is_dead())
            continue;
	    dead_heroes = false;

		if ( hero->get_spell_points() >= (2 * hero->get_maximum_spell_points() ) )
			continue;

		any_useable_heroes = true;

		t_creature_stack_ptr hero_stack_ptr = hero;

		hero_array.swap( hero_stack_ptr, i );
	}

    std::string status_text;
    if (any_heroes == false)
    {
        // No heroes
        status_text = get_text ("no_heroes");
        ok_dialog( status_text, get_dialog_bottom( point ) );
		play_music( music );
        return;
    }
    if (dead_heroes == true)
    {
	    status_text = get_text("heroes_dead");
        if (status_text == "")
            status_text = k_text_dead_heroes_ineligible;
		ok_dialog( status_text, get_dialog_bottom( point ) );
		play_music( music );
        return;
    }

    if (any_useable_heroes == false)
    {
        // All heroes already at twice-normal spell points
        status_text = get_text ("empty");
        ok_dialog( status_text, get_dialog_bottom( point ) );
		play_music( music );
        return;
    }

    // Select hero for benefit.
	t_counted_ptr<t_dialog_mana_vortex> dialog_ptr;

	dialog_ptr = new t_dialog_mana_vortex( frame );
	dialog_ptr->init_dialog( &hero_array, NULL, get_text ("Name"), get_text ("Initial"), get_text ("Accepted") );

    // Did hero get benefit?
	if (dialog_ptr->run_modal())
	{
	    m_mana_vortex_delay = k_mana_vortex_delay;
		frame->update_army( army );
	}
	play_music( music );
}

void t_adv_mana_vortex::process_new_day ()
{
	if (m_mana_vortex_delay > 0)
        m_mana_vortex_delay--;
}

int t_adv_mana_vortex::get_version() const
{
	return k_current_version;
}

bool t_adv_mana_vortex::read( std::streambuf&				stream, 
						  t_qualified_adv_object_type const&	type,
						  int									version )
{
	if (version < 1)
    {
        return true;
    }

	m_mana_vortex_delay = (int)( get<t_uint8> (stream));

    return true;
}

bool t_adv_mana_vortex::write( std::streambuf & stream ) const
{
	put< t_uint8 >( stream, m_mana_vortex_delay );

	return true;
}

